import sys
import time
import serial
#from visa import instrument

## CVI DK-480 Monochromator
class MC_DK480():
	
	def __init__(self, portNum='COM1'):
		##self.a_port = None
		self.stdTimeout = 2         # [sec] default timeout length
		self.fastTimeout = 0.5      # [sec] fast timeout length
		self.rundark = 0 # display deactivation
		self.COMport = portNum
		self.MC_ser = serial.Serial(self.COMport, 
		baudrate=9600, bytesize=8, timeout=0.5, xonxoff=1, rtscts=0)
		
	def openport(self):
		self.MC_ser = serial.Serial(self.COMport, 
		baudrate=9600, bytesize=8, timeout=0.5, xonxoff=1, rtscts=0)

    def closeport(self):
        self.MC_ser.close()

'''
#----------------------------------------------
# "Virtual" functions: no actual interface with instrument
#----------------------------------------------

    # set the selected output (default: 1) to a certain voltage
    def setSrc(self, volts, output=1):
        if output==1:
            self.volts1 = volts
            if self.state1==1: # immediate change if not an on/off switch
                self.update()
        elif output==2:
            self.volts2 = volts
            if self.state2==1: 
                self.update()
        else:
            return str('ERROR: invalid source specified')
        return

    # get the selected output (default: 1)'s voltage setting
    def getSrc(self, output=1):
        if output==1:
            return self.volts1
        elif output==2:
            return self.volts2
        else:
            return str('ERROR: invalid source specified')
        return


    # turn selected output (default: 1) on
    def turnOn(self, output=1):
        if output==1:
            self.state1 = 1
        elif output==2:
            self.state2 = 1
        else:
            return str('ERROR: invalid source specified')
        self.update()
        return

    # turn selected output (default: 1) off
    def turnOff(self, output=1):
        if output==1:
            self.state1 = 0
        elif output==2:
            self.state2 = 0
        else:
            return str('ERROR: invalid source specified')
        self.update()
        return
'''

#----------------------------------------------
# Lowest-level functions: wrappers for one-way communications
#----------------------------------------------

    # send commands to MC
    def write_byte(self, command=''):
        self.MC_ser.write(chr(command))
        return

	# read MC responses
	def read_byte(self):
		response = self.MC_ser.read()
		if response != "":
			return 0
		else:
			return ord(response)


#----------------------------------------------
# Low-level functions: two-way general communication
#----------------------------------------------

	# basic MC response check
	def echo(self):
		self.write(chr(27))
		ID = self.read()
		if ID==27:
			print "echoed " + ID
			return 1
		else:
			print 'ERROR: MC echo gave unexpected reponse!'
			return ID
'''
    # change a setting
    def setting(self, command):
        self.write(str(command) + '\n')
        # check for a response, which indicates command not accepted
        if not self.MC_ser.inWaiting()==0: 
            response = str(self.read())
            return response
        else:
            return 1

    # request a measurement
    def measure(self, command):
        self.write(str(command) + '\n')
        if not self.MC_ser.inWaiting()==0: 
            response = str(self.read())
            return response
        else:
            # uh-oh, nothing came back . . . 
            return -1
'''